/**
 * 
 */
package com.alianzamedica.controllers.prescription;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.web.tools.Converter;

import com.alianzamedica.businessobject.Doctor;
import com.alianzamedica.businessobject.Prescription;
import com.alianzamedica.businessobject.PrescriptionDetail;

/**
 * @author RMarquez_Inf
 * 
 */
public class EditDetailPrescriptionAction extends Action {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.
	 * ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		Prescription prescription = (Prescription) session
				.getAttribute("prescription");
		PrescriptionHelper util = new PrescriptionHelper();
		Integer detail = Converter.string2Integer(request
				.getParameter("detail"));
		Doctor doctor = (Doctor) session.getAttribute("doctor");
		String busqueda= request.getParameter("name");
		List list = util.showDrugs(doctor.getId(),busqueda);
		request.setAttribute("drugs", list);
		if (detail != null) {
			PrescriptionDetail prescriptionDetail = prescription.getDetail()
					.get(detail.intValue());
			request.setAttribute("prescriptionDetail", prescriptionDetail);
		} else {
			String idVal = request.getParameter("id");
			Integer id = Converter.string2Integer(idVal);
			if (id != null) {
				PrescriptionDetail pd = new PrescriptionDetail();
				pd.setDrugId(id);
				request.setAttribute("prescriptionDetail", pd);
			}
		}
		return mapping.findForward("success");
	}
}
